package com.group5.baseline.user.controller;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Set;

/**
 * @author tangtang
 * @create 2021-06-21 9:12
 */
@RestController
public class RedisController {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping("/setStringData")
    public String setStringData() {
        redisTemplate.opsForValue().set("think_string", "123456");
        return "success";
    }

    @GetMapping("/getStringData")
    public Object getStringData() {
        Object obj = redisTemplate.opsForValue().get("think_string");
        return obj;
    }

    @GetMapping("/setSetData")
    public String setSetData() {
        redisTemplate.opsForSet().add("think_set", "1", "2", "3", "4");
        return "success";
    }

    @GetMapping("/setListData")
    public String setListData() {
        redisTemplate.opsForList().leftPush("think_list", "a");
        redisTemplate.opsForList().leftPush("think_list", "b");
        redisTemplate.opsForList().leftPush("think_list", "c");  //  c b a
        return "success";
    }

    @GetMapping("/setMapData")
    public String setMapData() {
        redisTemplate.opsForHash().put("think_map", "map_key", "map_value");
        return "success";
    }

    @GetMapping("/setZSetData")
    public String setZSetData() {
        redisTemplate.opsForZSet().add("score","a1",80);
        redisTemplate.opsForZSet().add("score","b1",85);
        redisTemplate.opsForZSet().add("score","c1",90);
        return "success";
    }

    @GetMapping("/getZSetData")
    public Set<Object> getZSetData() {
        Set<Object> set = redisTemplate.opsForZSet().range("score",0,-1);
        return set;
    }
}
